﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using baseSet;
using System.Data.SqlClient;
using baseSet.importExport;
using dhSystemBLL.orderData;

namespace dhsystem.produceForm
{
    public partial class Frmprodutselect : Form
    {
       public  string  pname { get; set; }
       public string pnameI { get; set; }
        string tablename;
        string field;
        string fieldI="";

       // string queryValue;
        public Frmprodutselect(string _tablenam, string _field)
        {
            InitializeComponent();
            tablename = _tablenam;
            field = _field;
           // queryValue = _queryValue;
        }
        public Frmprodutselect(string _tablenam, string _field,string _fieldI)
        {
            InitializeComponent();
            tablename = _tablenam;
            field = _field;
            // queryValue = _queryValue;
            fieldI = _fieldI;
        }
        //打开窗口
        private void Frmprodutselect_Load(object sender, EventArgs e)
        {
           // textPnameQuery.Text = queryValue;
            baseSet.Query q = new baseSet.Query(null, dhDataAcess.baseBLL.dataConn.connOpen());
            bindingSourceselect.DataSource = q.sqlQueryLikeAll(tablename, textPnameQuery.Text.ToString().Trim());
            dGridslect.DataSource = bindingSourceselect;

            DcolumnSet.colSet(tablename , dGridslect );

            textPnameQuery.Focus();
            if (tablename == "supplierList" && (GlobalInfo.UserName == "7001" || GlobalInfo.UserName == "6001" || GlobalInfo.UserName == "8001"))
            {
                supplierListBLL supplierlist = new supplierListBLL();
                bindingSourceselect.DataSource = supplierlist.GetDatasupplierListBysTypeNo();
                dGridslect.DataSource = bindingSourceselect;
                DcolumnSet.colSet(tablename, dGridslect);
            }
        }
        //查询按钮
        private void buttonQuery_Click(object sender, EventArgs e)
        {

            dataQuery();
        }

        private void dGridslect_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        //确定按钮
        private void buttenter_Click(object sender, EventArgs e)
        {
            try
            {
                pname = dGridslect.CurrentRow.Cells[field].Value.ToString();
                if (fieldI != "")
                {
                    pnameI = dGridslect.CurrentRow.Cells[fieldI].Value.ToString();
                }
            }
            catch (Exception )
            {
                MessageBox.Show("选择正确的名称");
            }
        }
        //双击单元格
        private void dGridslect_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                pname = dGridslect.CurrentRow.Cells[field].Value.ToString();
                if (fieldI != "")
                {
                    pnameI = dGridslect.CurrentRow.Cells[fieldI].Value.ToString();
                }
                this.DialogResult = DialogResult.Yes;
            }
            catch (Exception   )
            {
                MessageBox.Show("选择正确的名称");
            }
        }
        //关闭按钮
        private void buttexit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //private void dGridDetail_CellEnter(object sender, DataGridViewCellEventArgs e)
        //{
        //    //dGridDetail.CurrentRow.Cells[e.ColumnIndex]
        //    DataGridViewCell cell = dGridDetail.CurrentRow.Cells[e.ColumnIndex];

        //}
        /// <summary>  
        /// 设置编辑状态下按回车键，跳到指定单元格.  
        /// </summary>  
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            string field;
            field = "matNum";
            if (keyData == Keys.Enter &&
                     (
                     (
                      !(ActiveControl is System.Windows.Forms.TextBox) ||
                      !((System.Windows.Forms.TextBox)ActiveControl).AcceptsReturn)
                     )
                     )   //监听回车事件   
            {
                //MessageBox.Show(ActiveControl.Name.ToString());
                if (ActiveControl == textPnameQuery)
                {
                    try
                    {
                        dataQuery();

                        return true;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }
                }
                if (this.dGridslect.CurrentCell.ColumnIndex == 1)
                {
                    try
                    {
                        pname = dGridslect.CurrentRow.Cells[field].Value.ToString();
                        this.DialogResult = DialogResult.Yes;
                    }
                    catch (Exception )
                    {
                        MessageBox.Show("选择正确的名称");
                    }
                }
                else
                {
                    SendKeys.Send("{Enter}");
                }

            }

            //继续原来base.ProcessCmdKey中的处理   
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void textPnameQuery_KeyDown(object sender, KeyEventArgs e)
        {

        }
        //查询语句
        private void dataQuery()
        {
            baseSet.Query q = new baseSet.Query(null, dhDataAcess.baseBLL.dataConn.connOpen());
            bindingSourceselect.DataSource = q.sqlQueryLikeAll(tablename, textPnameQuery.Text.ToString().Trim());
            dGridslect.DataSource = bindingSourceselect;
            DcolumnSet.colSet(tablename, dGridslect);
            dGridslect.Focus();
        }
    }
}
